package tuner;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

public class StringPanel extends JPanel{
	private JComboBox[] stringBoxes;
	
	private BufferedImage strings;
	private BufferedImage stringBase;
	
	private int[] stringHeights;
	private int[] stringCenters;
	
	private static char[] possibleLetters={'A','B','C','D','E','F','G'};
	
	public StringPanel(String tune)
	{
		super();
		this.setPreferredSize(new Dimension(200,200));
		this.setLayout(new MigLayout());
		
		initializeImages();
		
		String tuning=tune.substring(tune.indexOf('(')+1,tune.indexOf(')'));
		String[] selectedTuning=tuning.split(" ");
		int numStrings=selectedTuning.length;
		
		char[] myTuning=new char[numStrings];
		for(int i=0;i<numStrings;i++)
		{
			myTuning[i]=selectedTuning[i].charAt(0);
		}
		
		stringBoxes=new JComboBox[numStrings];
		stringHeights=new int[numStrings];
		stringCenters=new int[numStrings];
		
		for(int i=0;i<stringBoxes.length;i++)
		{
			//String noteOptions="";
			
			int start=-2;
			int end=6;
			String[] options=new String[end-start];
			//System.out.println(myTuning[i]);
			for(int n=0;n<end-start;n++)
			{
				int lettersIndex=indexOfNoteInLetters(myTuning[i]);
				int newIndex=lettersIndex+n+start;
				//System.out.println("newIndex: "+newIndex);
				try{
					newIndex=(newIndex+possibleLetters.length)%possibleLetters.length;
				}catch(Exception e)
				{
					newIndex=0;
				}
				options[n]=""+possibleLetters[newIndex];
				//System.out.println(newIndex);
			}
			stringBoxes[i]=new JComboBox(options);
			stringBoxes[i].setSelectedIndex((-1)*start);
			add(stringBoxes[i],"wrap");
		}
		//add(new JButton());
		
	}
	
	//WRITE THIS
	public void setNewBase(String tune)
	{
		
	}
	
	public int indexOfNoteInLetters(char note)
	{
		for(int i=0;i<possibleLetters.length;i++)
		{
			if(possibleLetters[i]==note)
			{
				return i;
			}
		}
		return -1;
	}
	
	public void initializeImages()
	{
		try{
			strings = ImageIO.read(StringPanel.class.getResource("/resources/images/guitarelements/guitarstrings/blackred/string1shadow.png"));
		    stringBase = ImageIO.read(StringPanel.class.getResource("/resources/images/guitarelements/guitarstrings/blackred/string1.png"));
		}catch(Exception e)
		{
		}
		
	}
	
	public void paint(Graphics g)
	{
		super.paint(g);
		drawStrings(g);
	}
	
	private void drawStrings(Graphics g)
	{ 
		
		   Dimension d = getSize();
		   int width = strings.getWidth();
		   int height = strings.getHeight();

		   for (int x = 0; x < d.width; x += width)
		   {
			   int counter=0;
			   for (int y = this.getHeight()/stringBoxes.length-this.getHeight()/(stringBoxes.length+3); y < this.getHeight(); y += this.getHeight()/stringBoxes.length)
			   {
				   stringHeights[counter]=y;
				   counter++;
				   g.drawImage( strings, x, y, null, null );
			   }
		   }
		   
		   for (int i=0;i<stringBoxes.length;i++)
		   {
			   stringCenters[i]=stringHeights[i]+stringBase.getHeight()/2;
		   }
	}
	
	public String getTuningStringFromSelection()
	{
		String toReturn="(";
		for(int i=0;i<stringBoxes.length;i++)
		{
			toReturn+=(String)stringBoxes[i].getSelectedItem();
			if(i!=stringBoxes.length-1)
			{
				toReturn+=" ";
			}
			else{
				toReturn+=")";
			}
		}
		return toReturn;
	}

}
